﻿using Database;
using System;
using System.Diagnostics;
using System.IO;
using UFrame;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;

namespace Database
{
}

public static class CfgOrmMenu
{

    private static bool isExecuteCMD = false;

    [MenuItem("Plateface/ORM/Generate CSVAndClass Two %&m", false, 1)]
    public static void CSVAndClassTwo()
    {
        string path = "";
        path = Application.dataPath + @"\..\..\CSVTools\exectuonTwo.bat";
        if (!File.Exists(path))
        {
            Debug.LogError("路径不存在: " + path);
            return;
        }
        Process.Start(path);
    }

    [MenuItem("Plateface/ORM/ReadORMCSV  %&ap", false, 1)]
    public static void ReadOrmTwo()
    {

        string path = "";
        path = Application.dataPath + @"\..\..\CSVTools\exectuonTwoCSV.bat";
        if (!File.Exists(path))
        {
            Debug.LogError("路径不存在: " + path);
            return;
        }

        var p = Process.Start(path);
        p.Exited -= ExitProcess;
        p.Exited += ExitProcess;
        p.EnableRaisingEvents = true;
        isExecuteCMD = false;
        EditorApplication.update -= EditorUpdate;
        EditorApplication.update += EditorUpdate;
    }

    [MenuItem("Plateface/ORM/ReadORMInMemory ", false, 1)]
    public static void ReadORMInMemory()
    {
        Type readType = AssemblyTool.SearchType("Database.CfgOrmRead");
        if (readType == null)
        {
            Debug.LogError("没有找到Database.CfgOrmRead类型");
            return;
        }

        CfgMa.isReadByte = true;
        var m = readType.GetMethod("LoadAll");
        //readType.GetField("isReadByte").SetValue(null,true);
        m.Invoke(null, null);
        Debug.Log("读取配置表完毕");
    }

    [MenuItem("Plateface/ORM/ORMWindow ", false, 1)]
    public static void OrmWindows()
    {
        OrmOperationWindow win = EditorWindow.GetWindow<OrmOperationWindow>(false, "ORM窗口", false);
        win.Show();
    }


    private static void ExitProcess(object sender, EventArgs e)
    {
        isExecuteCMD = true;
    }

    private static void EditorUpdate()
    {
        if (isExecuteCMD)
        {
            isExecuteCMD = false;
            ReadORMInMemory();
        }
    }
}